package com.shengruan.admin.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.vo.SliderLogo;
import com.shengruan.framework.jdbc.BaseJdbc;
import com.shengruan.framework.jdbc.BeanHandler;
import com.shengruan.framework.jdbc.BeanListHandler;

public class SliderLogoJdbc  extends BaseJdbc {

    Logger logger = LoggerFactory.getLogger(getClass());

    //添加
    public static final String SLIDERLOGO_INSERT = "INSERT INTO sliderlogo (pic, title, url, creationTime, status)VALUES(?, ?, ?, ?, ?)";
    public static final String SLIDERLOGO_UPDATE = "UPDATE sliderlogo SET pic = ? , title = ? , url = ? , creationTime = ?, status = ? WHERE id = ?";




    public void save(SliderLogo sliderLogo) {
        Connection conn = this.getConnection();
        Object[] pram = {sliderLogo.getPic(), sliderLogo.getTitle(), sliderLogo.getUrl(), sliderLogo.getCreationTime(), sliderLogo.getStatus()};
        try {
			this.execute(conn, SLIDERLOGO_INSERT, pram);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
	}

    
    public void update(SliderLogo sliderLogo) {
        Connection conn = this.getConnection();
        Object[] pram = {sliderLogo.getPic(), sliderLogo.getTitle(), sliderLogo.getUrl(), sliderLogo.getCreationTime(), sliderLogo.getStatus(),  sliderLogo.getId()};
        try {
			this.execute(conn, SLIDERLOGO_UPDATE, pram);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
	}


    public List<SliderLogo> findAll() {
        List<SliderLogo> sliderLogoList = new ArrayList<SliderLogo>();
        Connection conn = this.getConnection();
		String sql = "select * from sliderlogo order by creationTime desc";
		try {
			sliderLogoList = this.query(conn, sql, new BeanListHandler<SliderLogo>(SliderLogo.class));
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return sliderLogoList;
    }

    public List<SliderLogo> findAll(int status) {
        List<SliderLogo> sliderLogoList = new ArrayList<SliderLogo>();
        Connection conn = this.getConnection();
		String sql = "select * from sliderlogo s where s.status = ? order by creationTime desc";
        Object[] pram = {status};
		try {
			sliderLogoList = this.query(conn, sql, new BeanListHandler<SliderLogo>(SliderLogo.class), pram);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return sliderLogoList;
    }


     /**
     * 根据ID获得
     */
    public SliderLogo getById(int id) {
        Connection conn = this.getConnection();
        StringBuilder sql = new StringBuilder("SELECT * FROM sliderlogo s where s.id = ?");
		SliderLogo sliderLogo = null;
		try {
			sliderLogo = this.query(conn, sql.toString(), new BeanHandler<SliderLogo>(SliderLogo.class),id);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(conn);
		}
		return sliderLogo;
	}

    public void deleteById(int id) {
        Connection conn = this.getConnection();
        StringBuffer sql = new StringBuffer("delete from sliderlogo where id = ? ");
        try {
            this.execute(conn, sql.toString(), id);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
        }
    }
}
